package org.example.qqrobot.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.qqrobot.context.BaseContext;
import org.example.qqrobot.mapper.InvitationLabelMapper;
import org.example.qqrobot.mapper.InvitationMapper;
import org.example.qqrobot.mapper.UserMapper;
import org.example.qqrobot.model.dto.InvitationQueryDTO;
import org.example.qqrobot.model.pojo.Invitation;
import org.example.qqrobot.model.pojo.Label;
import org.example.qqrobot.model.pojo.Product;
import org.example.qqrobot.model.vo.InvitationIntoPageVO;
import org.example.qqrobot.model.vo.InvitationMainVO;
import org.example.qqrobot.model.vo.ProductMainVO;
import org.example.qqrobot.service.InvitationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class InvitationServiceImpl implements InvitationService {

    @Autowired
    private InvitationMapper invitationMapper;

    @Autowired
    private InvitationLabelMapper invitationLabelMapper;

    @Autowired
    private UserMapper userMapper;


    @Override
    public List<InvitationMainVO> query(InvitationQueryDTO invitationQueryDTO) {
        List<Invitation> iList = invitationMapper.query(invitationQueryDTO);
        List<InvitationMainVO> imList = new ArrayList<>();

        for (int i = 0; i < iList.size(); i++) {
            InvitationMainVO invitationMainVO = new InvitationMainVO();
            BeanUtils.copyProperties(iList.get(i),invitationMainVO);

            invitationMainVO.setInvitationLabel(invitationLabelMapper.findNameById(iList.get(i).getLabelId()));

            invitationMainVO.setName(userMapper.getNameById(invitationMapper.findWriterById(iList.get(i).getId())));
            imList.add(invitationMainVO);
        }

        return imList;
    }


    @Override
    public void save(String title, String content, String image) {
        Invitation invitation = new Invitation();
        invitation.setTitle(title);
        invitation.setYoulike(0L);
        invitation.setContent(content);
        invitation.setImage(image);
        invitation.setWriter(BaseContext.getCurrentId());
        invitation.setCreateTime(LocalDateTime.now());
        invitationMapper.save(invitation);

    }

    @Override
    public void like(Long invitationId, Long yourId) {

       Long like = invitationMapper.findLikeById(invitationId);

      like ++;


       invitationMapper.updateLikeById(like,invitationId);
    }

    @Override
    public InvitationIntoPageVO intoPage(Long id) {
        InvitationIntoPageVO InvitationIntoPageVO = new InvitationIntoPageVO();

        Invitation invitation =  invitationMapper.findById(id);
        BeanUtils.copyProperties(invitation,InvitationIntoPageVO);
        InvitationIntoPageVO.setName(userMapper.getNameById(invitation.getWriter()));
        return InvitationIntoPageVO;
    }

    @Override
    public List<InvitationMainVO> queryByName(String queryName) {
        //获得一个数组
        List<InvitationMainVO> pvList = new ArrayList<>();

        //原始的数据
        List<Invitation> pList = invitationMapper.queryByName(queryName);



        for (int i = 0; i < pList.size(); i++) {
            InvitationMainVO invitationMainVo = new InvitationMainVO();
            BeanUtils.copyProperties(pList.get(i),invitationMainVo);
            invitationMainVo.setName(userMapper.getNameById(pList.get(i).getWriter()));
            //invitationMainVo.setLabelList(productLabelMapper.findByProductId(pList.get(i).getId()));
            pvList.add(invitationMainVo);

        }

        return pvList;

    }


    @Override
    public List<InvitationMainVO> lookMyInvitation() {
        List<Invitation> pList = invitationMapper.findByWriterId(BaseContext.getCurrentId());
        List<InvitationMainVO> pmList = new ArrayList<>();
        //获取product
        for (int i = 0; i < pList.size(); i++) {

            InvitationMainVO invitationMainVO = new InvitationMainVO();
            BeanUtils.copyProperties(pList.get(i),invitationMainVO);

            // List<Label> lList = productLabelMapper.findByProductId(pList.get(i).getId());

            //productMainVO.setLabelList(lList);
            invitationMainVO.setName(userMapper.getNameById(pList.get(i).getWriter()));

            pmList.add(invitationMainVO);


        }

        return pmList;

    }
}

